/*
 * Copyright (c) 2003, 2004 Ulrich Hilger, http://dev.uhilger.de, all rights reserved.
 *
 * Published under the terms and conditions of the Apache License 2.0,
 * see http://www.apache.org/licenses/LICENSE-2.0
 */

package de.uhilger.app;

import javax.swing.UIManager;
import java.util.logging.Level;
import java.awt.Frame;
import javax.swing.JFrame;
import java.io.OutputStream;
import de.uhilger.app.ui.MainFrame;
import de.uhilger.app.action.ExitAction;
import de.uhilger.app.action.AboutAppAction;
import de.uhilger.app.action.HelpContentsAction;

/**
 * Default application class
 *
 * <p>This class can be used as an example for how to make use of the Light
 * Development Application Package. It extends the abstract base class
 * <code>MenuApplication</code> which in turn extends <code>Application</code>
 * and initializes some basic elements such as
 * a resource bundle or an action to exit the application.</p>
 *
 * @author Ulrich Hilger, <a href="http://dev.uhilger.de" target="_blank">http://dev.uhilger.de</a>
 * @author Published under the terms and conditions of
 * the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License 2.0</a>
 *
 * @version 2, February 12, 2004
 */

public class DefaultApplication extends MenuApplication {

  /**
   * constructor
   */
  public DefaultApplication () {
    super();
    initMainFrame(true);
    log(getResourceString(Application.RB_KEY_LOG_APP_STARTED), getClass(), Level.INFO);
  }

  public DefaultApplication(Level level, String logDir) {
    this(System.out, level, logDir, true);
  }

  public DefaultApplication(Level level, String logDir, boolean showUi) {
    this(System.out, level, logDir, showUi);
  }

  public DefaultApplication(OutputStream logStream, Level level, String logDir, boolean showUi) {
    super(logStream, level, logDir);
    initMainFrame(showUi);
    log(getResourceString(Application.RB_KEY_LOG_APP_STARTED), getClass(), Level.INFO);
  }

  /**
   * initialize this application's action table
   */
  protected void initActions() {
    Class c = getClass();
    addAction(c, ACTION_EXIT_APP, new ExitAction(this));
    addAction(c, ACTION_ABOUT_APP, new AboutAppAction(this));
    addAction(c, ACTION_SHOW_HELP_CONTENTS, new HelpContentsAction());
  }

  /**
   * initialize this application's main frame
   */
  protected void initMainFrame(boolean showUi) {
    // create the application's main frame
    MainFrame mf = new MainFrame(this, getAppName());

    if (showUi) {
      mf.setLocationRelativeTo(null);
    }
    mf.setVisible(showUi);
  }

  /*
   not needed, see method java.awt.Window.setLocationRelativeTo

   protected void centerFrame(Frame mf) {
    //Center the main frame
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = mf.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    mf.setLocation( (screenSize.width - frameSize.width) / 2,
                   (screenSize.height - frameSize.height) / 2);
  }*/

  /**
   * not needed, see method java.awt.Window.setLocationRelativeTo
   *
   * center a <code>Component</code> relative to
   * another <code>Component</code>.
   *
   * @param parent  the <code>Component</code> to be used as the
   *                  basis for centering
   * @param comp  the <code>Component</code> to be centered within parent
   *
   */
  /*protected void center(Component parent, Component comp) {
    Dimension cSize = comp.getSize(); // .getPreferredSize();
    Dimension fSize = parent.getSize();
    Point loc = parent.getLocation();
    comp.setLocation((fSize.width - cSize.width) / 2 + loc.x,
                            (fSize.height - cSize.height) / 2 + loc.y);
  }*/

  /**
   * find the application's main frame
   *
   * @return MainFrame the main frame of this application or null, if none was found
   */
  public static MainFrame findMainFrame() {
    /* find the Application's main frame */
    MainFrame mf = null;
    Frame[] frames = JFrame.getFrames();
    int i = 0;
    while (i < frames.length && mf == null) {
      if (frames[i] instanceof MainFrame) {
        mf = (MainFrame) frames[i];
      }
      i++;
    }
    return mf;
  }

  /**
   * main method
   * @param args String[] command line arguments
   */
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    new DefaultApplication();
  }

  /* ------------------- class fields start ------------------------ */

  /** name of the action that exits this application */
  public static final String ACTION_EXIT_APP = "exitApp";

  /** name of the action that shows information about this application */
  public static final String ACTION_ABOUT_APP = "aboutApp";

  /** name of the action that shows help for this application */
  public static final String ACTION_SHOW_HELP_CONTENTS = "showHelpContents";

  /* ------------------- class fields end ------------------------ */

}
